package com.blog.Bard.base.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.blog.Bard.base.pojo.BardMenu;
import com.blog.Bard.common.dao.BaseRepository;

public interface BardMenuDao extends BaseRepository<BardMenu, Integer>{
	
	/**
	 * 查找所有一级菜单
	 * @return
	 * @throws Exception
	 */
	@Query(nativeQuery = true, value = " select * from bardmenu \n"
			+ " where level = 1 and menuStatus = 1  \n"
			+ " order by seq ")
	List<BardMenu> getParentMenuList() throws Exception;
	
	/**
	 * 菜单列表
	 * @return
	 * @throws Exception
	 */
	@Query(nativeQuery = true, value = "select level, menuName name, menuCode code, url from bardmenu \n"
			+ " where level = 2 and menuStatus = 1  and parentCode = :parentCode \n"
			+ " order by seq ")
	List<Map<String,Object>> getChildMenuList(@Param("parentCode") String parentCode) throws Exception;
	
	/**
	 * 获取子级菜单
	 * @param parentCode
	 * @return
	 * @throws Exception
	 */
	@Query(nativeQuery = true, value = "select id, menuName title from bardmenu \n"
			+ " where level = 2 and menuStatus = 1  and parentCode = :parentCode \n"
			+ " order by seq ")
	List<Map<String,Object>> getChildMenuTrees(@Param("parentCode") String parentCode) throws Exception;
	
	/**
	 * 	获取最新的序号
	 * @param parentCode
	 * @return
	 * @throws Exception
	 */
	@Query(nativeQuery = true, value = " select IFNULL((max(seq) + 1), 1) nextSeq FROM bardmenu m where m.parentCode = :parentCode \n")
	Map<String, Object> getNextSeq(@Param("parentCode") String parentCode) throws Exception;

}
